/**
 * Created by PanJiaChen on 16/11/18.
 */

/**
 * @param {string} path
 * @returns {Boolean}
 */
export function isExternal(path) {
  return /^(https?:|mailto:|tel:)/.test(path)
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validUsername(str) {
  const valid_map = ['admin', 'editor']
  return valid_map.indexOf(str.trim()) >= 0
}

export function nameMask(name) {
  if (!name) return ''
  const len = name.length
  const symbolFactory = function(length) {
    let result = ''
    for (let i = 0; i < length; i++) {
      result += '*'
    }
    return result
  }
  if (/[\u4e00-\u9fa5]/.test(name)) {
    // 纯中文
    switch (len) {
      case 1:
        // eslint-disable-next-line no-self-assign
        name = name
        break
      case 2:
        name = `${name.charAt(0)}*`
        break
      default:
        name = `${name.charAt(0)}*${name.charAt(len - 1)}`
    }
  } else {
    // 非中文
    if (len > 7) {
      const isEven = len % 2 === 0
      const ceilVal = Math.ceil(len * 0.3)
      if (isEven) {
        // 偶数
        const front = ceilVal
        const middle = len % 3 === 0 ? len / 3 : Math.floor(len / 3) + 1
        const end = front + middle
        name = name.slice(0, front) + symbolFactory(middle) + name.slice(end)
      } else {
        // 奇数
        const front = ceilVal
        const middle = ceilVal
        const end = ceilVal * 2
        name = name.slice(0, front) + symbolFactory(middle) + name.slice(end)
      }
    } else {
      const ceilVal = Math.ceil(len * 0.5)
      name = name.slice(0, ceilVal) + symbolFactory(len - ceilVal)
    }
  }
  return name
}

/**
 * 常用的正则校验方法
 * @param {*} regExpType 需要校验的表单类型
 * @param {*} inputVal 需要校验的表单内容
 * @param {*} valid 是否自动校验 （默认为true）
 * @returns boolean | regexp
 */
export function regExpValid({ regExpType = '', inputVal = '', valid = true }) {
  if (!regExpType) return false
  const _regExpMap = {
    phone: /^1\d{10}$/,
    wx: /^[-_a-zA-Z0-9]{6,20}$/,
    email: /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/,
    userName: /^([a-zA-Z]|[\u4e00-\u9fa5\·]){2,15}$/,
    amount: /^[0-9]+(.[0-9]{2})?$/,
    vCode: /^\d{6}$/,
    password: /^[a-zA-Z0-9_]{6,16}$/,
    url: /^((ht|f)tps?):\/\/[\w\-]+(\.[\w\-]+)+([\w\-\.,@?^=%&:\/~\+#]*[\w\-\@?^=%&\/~\+#])?$/,
    idCard: /(^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$)|(^[1-9]\d{5}\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{2}$)/
  }
  if (!valid) return _regExpMap[regExpType]
  return _regExpMap[regExpType].test(inputVal)
}

// 数字脱敏
export function desenStr(str, type, show = false) {
  if (!str) return '-'
  if (show) return str
  switch (type) {
    case 'tel':
      return str.replace(new RegExp('(\\d{3})\\d*(\\d{4})'), '$1****$2')
    case 'email':
      return str.split('@')[0].length >= 4 ? str.replace(new RegExp('(?<=.{2})[^@]+(?=.{2}@)'), '****')
        : str.replace(new RegExp('(?<=.{1})[^@]+(?=@)'), '****')
    case 'idCard':
      return str.replace(new RegExp('(\\d{4})\\d{10}(\\w{4})'), '$1*****$2')
    case 'name':
      return nameMask(str)
    default:
      return str || '-'
  }
}
